/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.studio.persistence.view.wizards;


import org.apache.directory.studio.persistence.core.CodeGeneratorContext;
import org.apache.directory.studio.persistence.ui.Activator;
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;


public class NewPersistenceCodeWizardPage extends WizardPage
{
    private Text classNameText;
    private Text packageNameText;
    private Text projectFolderText;
    private Button projectFolderButton;

    private CodeGeneratorContext codeGenContext;


    protected NewPersistenceCodeWizardPage()
    {
        super( "NewPersistenceCodeWizardPage" );
        setTitle( "Java Class" );
        setDescription( "Create a new Java Class from an existing LDAP entry." );
        // setImageDescriptor( //Image to be added );
        setPageComplete( false );
    }


    public void createControl( Composite parent )
    {
        Composite composite = new Composite( parent, SWT.NULL );
        GridLayout layout = new GridLayout();
        composite.setLayout( layout );

        // Set Class name and package name
        Group classPackageGroup = new Group( composite, SWT.NONE );
        classPackageGroup.setText( "Set class name and package" );
        classPackageGroup.setLayout( new GridLayout( 2, false ) );
        classPackageGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );

        //name of the package
        Label packageNameLabel = new Label( classPackageGroup, SWT.NONE );
        packageNameLabel.setText( "Package Name:" );
        packageNameText = new Text( classPackageGroup, SWT.BORDER );
        packageNameText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        //name of the class
        Label classNameLabel = new Label( classPackageGroup, SWT.NONE );
        classNameLabel.setText( "Class Name:" );
        classNameText = new Text( classPackageGroup, SWT.BORDER );
        classNameText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        initFields();

        packageNameText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        classNameText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );

        setControl( composite );

        // Select Project Folder Group
        Group projectFolderGroup = new Group( composite, SWT.NONE );
        projectFolderGroup.setText( "Select project Folder" );
        projectFolderGroup.setLayout( new GridLayout( 3, false ) );
        projectFolderGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );

        // Project Folder
        Label fromDirectoryLabel = new Label( projectFolderGroup, SWT.NONE );
        fromDirectoryLabel.setText( "To Project Folder:" );
        projectFolderText = new Text( projectFolderGroup, SWT.BORDER );
        projectFolderText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
        projectFolderText.addModifyListener( new ModifyListener()
        {
            public void modifyText( ModifyEvent e )
            {
                dialogChanged();
            }
        } );
        projectFolderButton = new Button( projectFolderGroup, SWT.PUSH );
        projectFolderButton.setText( "Browse" );
        projectFolderButton.addSelectionListener( new SelectionAdapter()
        {
            public void widgetSelected( SelectionEvent e )
            {
                setProjectFolder();
            }
        } );
        dialogChanged();
    }


    private void initFields()
    {
        packageNameText.setText( codeGenContext.getPackageName() );
        classNameText.setText( codeGenContext.getClassName() );
    }


    public void showErrorMessage( String message )
    {
        setMessage( null, DialogPage.NONE );
        setErrorMessage( message );
        setPageComplete( message == null );
    }


    private void dialogChanged()
    {
        if ( projectFolderText.getText().isEmpty() )
        {
            showErrorMessage( "No Project Folder is Selected." );
            return;
        }
        else
        {
            setPageComplete( true );
        }
        codeGenContext.setClassName( classNameText.getText() );
        codeGenContext.setPackageName( packageNameText.getText() );
        //set the path is changed
        codeGenContext.setProjectFolderPath( projectFolderText.getText() );
        
        showErrorMessage( null );
    }


    /**
     * @param entryInformation the entryInformation to set
     */
    public void setEntryInformation( CodeGeneratorContext codeGenContext )
    {
        this.codeGenContext = codeGenContext;
    }


    public void setProjectFolder()
    {
        DirectoryDialog dialog = new DirectoryDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() );
        dialog.setText( "Set Project Folder" );
        dialog.setMessage( "Select Project Folder for generating code" );
        if ( "".equals( projectFolderText.getText() ) )
        {
            dialog.setFilterPath( Activator.getDefault().getPreferenceStore().getString( "/home/kasun/eclipse" ) );
        }
        else
        {
            dialog.setFilterPath( projectFolderText.getText() );
        }

        String selectedDirectory = dialog.open();
        if ( selectedDirectory != null )
        {
            projectFolderText.setText( selectedDirectory );
            codeGenContext.setProjectFolderPath( selectedDirectory + "/src/main/java" );
        }
    }

}
